<template>
	<view class="page-container">
		<view class="nav-bar">
			<view class="back-btn" @click="goBack">←</view>
			<view class="page-title">修改老人信息</view>
		</view>

		<view class="MainBox">
			<view class="form-list">
				<view class="form-item">
					<view class="label">姓名</view>
					<input class="input" v-model="userInfo.name" placeholder="请输入姓名" />
				</view>

				<view class="form-item">
					<view class="label">年龄</view>
					<input class="input" v-model="userInfo.age" placeholder="请输入年龄" type="number" maxlength="3" />
				</view>

				<view class="form-item">
					<view class="label">性别</view>
					<input class="input" v-model="userInfo.gender" placeholder="请输入性别" maxlength="2" />
				</view>

				<view class="form-item">
					<view class="label">病历</view>
					<input class="input" v-model="userInfo.medicalRecord" placeholder="请输入病历" />
				</view>

				<view class="form-item">
					<view class="label">用药注意</view>
					<input class="input" v-model="userInfo.medicationPrecautions" placeholder="请输入用药注意" />
				</view>
			</view>
		</view>

		<button class="save-btn" @click="saveInfo">保存修改</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					name: '张**',
					age: '89',
					gender: '女',
					medicalRecord: '糖尿病、高血压、冠心病',
					medicationPrecautions: '早上 7:00：降压药 1 片（空腹）,中午 12:30：降糖药 1 片（饭后 30 分钟）晚上 8:00：降脂药 1 片（睡前）忌辛辣、高糖、高盐食物'
				}
			};
		},
		onLoad() {
			const pages = getCurrentPages();
			const prevPage = pages[pages.length - 2];
			if (prevPage.data.userInfo) {
				this.userInfo = {
					...prevPage.data.userInfo
				};
			}
		},
		methods: {
			goBack(){
				uni.navigateBack();
			},
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.avatarUrl = res.tempFilePaths[0];
					}
				});
			},
			saveInfo() {
				if (!this.userInfo.name.trim()) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return;
				}
				if (!this.userInfo.medicationPrecautions.trim() || this.userInfo.medicationPrecautions.length !== 11) {
					uni.showToast({
						title: '请输入正确的用药注意',
						icon: 'none'
					});
					return;
				}

				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});

				setTimeout(() => {
					const pages = getCurrentPages();
					const prevPage = pages[pages.length - 2];
					prevPage.setData({
						userInfo: this.userInfo,
						avatarUrl: this.avatarUrl
					});
					uni.navigateBack({
						delta: 1
					});
				}, 1000);
			}
		}
	};
</script>

<style scoped>
	.page-container {
		background-color: #fff8f0;
		min-height: 100vh;
		padding-bottom: 30px;
	}

	.nav-bar {
		height: 80px;
		background-color: #fff8f0;
		display: flex;
		align-items: center;
		padding: 0 15px;
		border-bottom: 1px solid #ffd5b8;
	}

	.back-btn {
		font-size: 20px;
		color: #ff7d00;
	}

	.page-title {
		font-size: 18px;
		font-weight: 600;
		color: #ff7d00;
		margin-left: 10px;
	}

	.MainBox {
		background-color: #fff;
		border-radius: 12px;
		padding: 20px 15px;
		margin: 15px;
		box-shadow: 0 2px 8px rgba(255, 125, 0, 0.08);
	}

	.form-list {
		margin-top: 10px;
	}

	.form-item {
		display: flex;
		align-items: center;
		padding: 12px 0;
		border-bottom: 1px solid #fff8f0;
	}

	.form-item:last-child {
		border-bottom: none;
	}

	.label {
		font-size: 15px;
		color: #666;
		width: 80px;
	}

	.input {
		font-size: 15px;
		color: #333;
		flex: 1;
		padding: 5px 0;
		border: none;
		outline: none;
	}

	.input::placeholder {
		color: #e0a872;
		font-size: 14px;
	}

	.save-btn {
		height: 60px;
		display: flex;
		justify-content: center;
		align-items: center;
		width: calc(100% - 30px);
		margin: 0 auto;
		padding: 14px 0;
		background-color: #ff7d00;
		color: #fff;
		border: none;
		border-radius: 12px;
		font-size: 16px;
		box-shadow: 0 4px 12px rgba(255, 125, 0, 0.15);
	}

	.save-btn:active {
	/* 	opacity: 0.9;
		transform: scale(0.98); */
	}
</style>